﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class OrderService : EntityService<Order>
    {
        public List<Order> GetAllOrders()
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    orderby order.CreationDate ascending
                    select order).ToList();
        }

        public List<Order> GetAllOrders_QuotationRpt()
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.HasQuotation == true && order.IsValid == true
                    orderby order.QuotationCreationDate, order.QuotationNo
                    select order).ToList();
        }

        public List<Order> GetSpecificCompanyOrders(String CompanyType)
        {
            List<Order> resultList = (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                                        where order.CompanyType == CompanyType
                                        orderby order.QuotationNo
                                        select order).ToList();

            if(CompanyType == "7")
            {
                resultList = (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                              where order.CompanyType == CompanyType
                              orderby order.ServiceOrderNo
                              select order).ToList();
            }

            return resultList;
        }

        public List<Order> GetOrders_ByServiceNO(String ServiceNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.ServiceOrderNo == ServiceNo
                    orderby order.CreationDate
                    select order).ToList();
        }

        public Order GetOrder_ByServiceNO(String ServiceNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.ServiceOrderNo == ServiceNo
                    orderby order.CreationDate
                    select order).FirstOrDefault();
        }

        public List<Order> GetOrders_ByWorkOrderNO(String WorkOrderNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.WorkOrderNo == WorkOrderNo
                    orderby order.CreationDate
                    select order).ToList();
        }

        public List<Order> GetOrders_ByServiceNOAndWorkOrderNO(String ServiceNo, String WorkOrderNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.ServiceOrderNo == ServiceNo && order.WorkOrderNo == WorkOrderNo
                    orderby order.CreationDate
                    select order).ToList();
        }

        public List<Order> GetOrders_ByFMCode(String FMCode)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.FMCode == FMCode && order.FMCode != null
                    orderby order.CreationDate
                    select order).ToList();
        }

        public List<Order> GetOrders_ByQuotationNo(String QuotationNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.QuotationNo == QuotationNo
                    orderby order.CreationDate
                    select order).ToList();
        }

        public Order GetOrder_ByQuotationNo(String QuotationNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.QuotationNo == QuotationNo
                    select order).FirstOrDefault();
        }

        public List<Order> GetOrders_ByFMCodeAndQuotationNo(String FMCode, String QuotationNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.FMCode == FMCode && order.FMCode != null && order.QuotationNo.Contains(QuotationNo)
                    orderby order.QuotationNo
                    select order).ToList();
        }

        public int GetNextAvailableQuotationNo(String companyType)
        {
            int Num = 0;
            List<Order> AllOrdersList = GetSpecificCompanyOrders(companyType).OrderBy(x => x.QuotationNo).ToList();

            if (AllOrdersList.Count > 0 && companyType == "3")
            {
                String[] split = AllOrdersList[AllOrdersList.Count - 1].QuotationNo.Split('r');
                Num = int.Parse(split[0]) + 1;
            }
            else
                Num++;

            return Num;
        }

        public String GetNextQuotationNo(String CompanyType)
        {
            int QuoteNo = 1;
            String NewQuoteNo = "";

            List<String> CurrentQuoteNoList = (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                                               where order.QuotationNo != "" && order.QuotationNo != "" && order.CompanyType == CompanyType
                                               orderby order.QuotationNo descending
                                               select order.QuotationNo).ToList();

            if (CurrentQuoteNoList.Count == 0)
                NewQuoteNo = "AH-" + DateTime.Today.ToString("MMyy") + "-" + QuoteNo.ToString("0000");
            else
            {
                List<int> QuoteNoList = GetQuoteNoList(CurrentQuoteNoList);
                QuoteNo = QuoteNoList[QuoteNoList.Count - 1] + 1;
                NewQuoteNo = "AH-" + DateTime.Today.ToString("MMyy") + "-" + QuoteNo.ToString("0000");
            }

            return NewQuoteNo;
        }

        public String GetCurrentWorkOrderNo(String ServiceOrderNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.ServiceOrderNo == ServiceOrderNo
                    select order.WorkOrderNo).FirstOrDefault();
        }

        public String GetCurrentQuotationNo(String ServiceOrderNo)
        {
            return (from order in this.MongoConnectionHandler.MongoCollection.FindAllAs<Order>()
                    where order.ServiceOrderNo == ServiceOrderNo
                    select order.QuotationNo).FirstOrDefault();
        }

        private List<int> GetQuoteNoList(List<String> QuoteNoListInSTring)
        {
            List<int> resultList = new List<int>();

            foreach (String strnQuoteNo in QuoteNoListInSTring)
            {
                String[] splitList = strnQuoteNo.Split('-');
                int tempNo = int.Parse(splitList[2]);
                resultList.Add(tempNo);
            }

            resultList.Sort();

            return resultList;
        }

        public bool CreateOrders(List<Order> orderList)
        {
            bool isSuccessful = false;

            try
            {
                foreach (Order order in orderList)
                {
                    Order tempOrder = order;
                    tempOrder.CreationDate = DateTime.Now;
                    tempOrder.HasInvoice = false;
                    tempOrder.InvoiceCreationDate = new DateTime();
                    tempOrder.InvoiceNo = "";

                    if (tempOrder.CompanyType == "7")
                    {
                        tempOrder.QuotationNo = "";
                        tempOrder.HasQuotation = false;
                        tempOrder.QuotationCreationDate = new DateTime();
                    }
                    
                    tempOrder.VendorNo = "725369";
                    tempOrder.IsValid = true;

                    this.Create(tempOrder);
                }

                isSuccessful = true;
            }
            catch (Exception e)
            {
                isSuccessful = false;
            }

            return isSuccessful;
        }

        public void UpdateIsValid(String QuotationNo, bool isValid)
        {
            var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                Query<Order>.EQ(o => o.QuotationNo, QuotationNo),
                Update<Order>.Set(o => o.IsValid, isValid));
        }

        public void UpdateInvoiceNumber(String ServiceOrderNo, String InvoiceNo, DateTime InvoiceCreationDate)
        {
            var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                Query<Order>.EQ(o => o.ServiceOrderNo, ServiceOrderNo),
                Update<Order>.Set(o => o.InvoiceNo, InvoiceNo)
                .Set(o => o.InvoiceCreationDate, InvoiceCreationDate)
                .Set(o => o.HasInvoice, true));
        }

        public void UpdateInvoiceNumberJLLPO(String QuotationNo, String InvoiceNo, DateTime InvoiceCreationDate)
        {
            var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                Query<Order>.EQ(o => o.QuotationNo, QuotationNo),
                Update<Order>.Set(o => o.InvoiceNo, InvoiceNo)
                .Set(o => o.InvoiceCreationDate, InvoiceCreationDate)
                .Set(o => o.HasInvoice, true));
        }

        public override void Update(Order entity)
        {
            if (entity.CompanyType == "7")
            {
                this.MongoConnectionHandler.MongoCollection.Update(
                    Query<Order>.EQ(o => o.ServiceOrderNo, entity.ServiceOrderNo),
                    Update<Order>.Set(o => o.WorkOrderNo, entity.WorkOrderNo)
                    .Set(o => o.QuotationNo, entity.QuotationNo)
                    .Set(o => o.QuotationCreationDate, entity.QuotationCreationDate)
                    .Set(o => o.HasQuotation, entity.HasQuotation));
            }
            else if (entity.CompanyType == "3")
            {
                this.MongoConnectionHandler.MongoCollection.Update(
                    Query<Order>.EQ(o => o.QuotationNo, entity.QuotationNo),
                    Update<Order>.Set(o => o.WorkOrderNo, entity.WorkOrderNo)
                    .Set(o => o.QuotationNo, entity.QuotationNo)
                    .Set(o => o.QuotationCreationDate, entity.QuotationCreationDate)
                    .Set(o => o.HasQuotation, entity.HasQuotation));
            }
        }
    }
}